package pers.cz.obj;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: PostGirl-panent
 * @description: 对象工具
 * @author: Cheng Zhi
 * @create: 2022-04-24 21:58
 **/
public class ObjectUtils {

    /**
     * 获取所有对象属性名称
     * @param o
     * @return
     */
    public static String[] getFiledNames(Object o) {
        Field[] fields=o.getClass().getDeclaredFields();
        String[] fieldNames=new String[fields.length];
        for(int i=0;i<fields.length;i++){
            fieldNames[i]=fields[i].getName();
        }
        return fieldNames;
    }

    /**
     * 获取对象属性值
     * @param o
     * @return
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static String[] getFieldValues(Object o) {
        Field[] fields=o.getClass().getDeclaredFields();
        String[] fieldNames=new String[fields.length];
        String[] fieldValues = new String[fieldNames.length];
        for(int i=0;i<fields.length;i++){
            fieldNames[i]=fields[i].getName();
        }
        try {
            for (int i=0; i<fieldNames.length; i++) {
                String fieldName = fieldNames[i];
                String field = o.getClass().getMethod("get" + returnFirstCapital(fieldName)).invoke(o).toString();
                fieldValues[i] = field;
            }
        } catch(Exception e) {

        }
        return fieldValues;
    }

    public static void getFieldNameAndValue(Object o, Map<String, Object> map) {

        if (o instanceof Map) {

            for (Object keyObject : ((Map) o).keySet()) {
                Object value = ((Map) o).get(keyObject);

                if (value instanceof ArrayList) {
                    for (Object listObj : (ArrayList)value) {
                        getFieldNameAndValue(listObj, map);
                    }
                }
            }
        } else {

            Field[] fields=o.getClass().getDeclaredFields();
            String[] fieldNames=new String[fields.length];
            String[] fieldValues = new String[fieldNames.length];
            for(int i=0;i<fields.length;i++){
                fieldNames[i]=fields[i].getName();
            }
            try {
                for (int i=0; i<fieldNames.length; i++) {
                    String fieldName = fieldNames[i];
                    Method method = o.getClass().getMethod("get" + returnFirstCapital(fieldName));
                    Object invoke = method.invoke(o);
                    if (invoke != null) {
                        map.put(camel2under(fieldName), invoke);
                    }
                }
            } catch(Exception e) {

            }
        }
    }

    /**
     * 将驼峰命名转换为下划线命名
     * @param c
     * @return
     */
    private static String camel2under(String c) {
        String separator = "_";
        c = c.replaceAll("([a-z])([A-Z])", "$1"+separator+"$2").toUpperCase();
        return c;
    }

    /**
     * 判断字符串首字母是否为大写，如果不是转化为大写
     * @param str
     * @return
     */
    public static String returnFirstCapital(String str) {
        if (str.charAt(0) >= 'A' && str.charAt(0) <= 'Z') {
            return str;
        }
        char[] ch = str.toCharArray();
        ch[0] -= 32;
        return String.valueOf(ch);
    }

    public static void main(String[] args) {
        ObjectUtils objectUtils = new ObjectUtils();
        System.out.println(objectUtils.camel2under("soNbr"));
    }
}
